package com.diqizu.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ArrayList;
import com.diqizu.entity.Users;
import com.diqizu.utli.DBManager;
import com.diqizu.utli.QueryDataResult;

public class Users_Method {
	

	/**
	 * 单查--用户名和密码
	 * @param userName
	 * @param userPwd
	 * @return
	 */
	public Users getUsers(String userName,String userPwd) {
		String sql = "select * from users where userName='"+userName+"' and userPwd='"+userPwd+"' ";
		System.out.println(sql);
		QueryDataResult executeQuery = DBManager.execQuery(sql);
		if(executeQuery != null) {
			ResultSet rs = executeQuery.rs;
			try {
				while(rs.next()) {
					Users user = new Users();
					user.setUserName(rs.getString("userName"));
					user.setUserPwd(rs.getString("userPwd"));
					user.setUserUrl(rs.getString("userUrl"));
					System.out.println("获取的数据是"+user);
					return user;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				executeQuery.close();
			}
		}
		return null;
	}
	
	/**
	 * 修改密码
	 * @param 
	 * @return
	 */
	public boolean updUsers(String renewpass,String userName) {
		String sql = "update users set userPwd='"+renewpass+"'where userName='"+userName+"'";
		System.out.println(sql);
		boolean i = DBManager.execUpdate(sql);
		if(i==true) {
			System.out.println("修改成功");
			return true;
		}else {
			System.out.println("修改失败");
			return false;
		}
		
	}
	
	/**
	 * 查询
	 * @param 
	 * @return
	 */
	public Users Users(String userName,String previousPwd) {

		String sql = "select * from users where userName='"+userName+"',userPwd='"+previousPwd+"'";

		System.out.println(sql);
		QueryDataResult executeQuery = DBManager.execQuery(sql);
		if(executeQuery != null) {
			ResultSet rs = executeQuery.rs;
			try {
				while(rs.next()) {
					Users user = new Users();
					user.setUserName(rs.getString("userName"));
					user.setUserPwd(rs.getString("userPwd"));
					user.setUserUrl(rs.getString("userUrl"));
					return user;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				executeQuery.close();
			}
		}
		return null;
	}
}
